<?php

namespace IDock\Insurance\ZFIC\Response;

use IDock\Http\Response;
use IDock\Printer\NonCar\NonCarProductListPrinter;
use IDock\Printer\NonCar\NonCarProductPrinter;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;
use IDock\Printer\UnderwritingRecallPrinter;

class NonCarProductListResponse extends AllZFICResponse
{

    public function printerInit(): Response
    {
        $printer = new NonCarProductListPrinter();

        $list = $this->get('data' , []);

        foreach ($list as $item) {
            $amount = $premium = '0';
            $productPrinter = new NonCarProductPrinter();
            $productPrinter->productCode = $item['productCode'] ?? '';
            $productPrinter->productName = $item['productName'] ?? '';
            $productPrinter->productType = '驾意险';
            $productPrinter->productDesc = '';
            $productPrinter->planCode    = $item['planCode'] ?? '';
            $productPrinter->planName    = $item['planName'] ?? '';
            $productPrinter->maxQuantity = (string)($item['maxCount'] ??  1);

            foreach ($item['customerKindVo'] ?? [] as $sub) {
                $subCoverage = new NonCarSubCoveragePrinter();
                $subCoverage->subCode = $sub['kindCode'] ?? '';
                $subCoverage->subName = $sub['kindName'] ?? '';
                $subCoverage->amount  = $sub['unitAmount'] ?? '';
                $subCoverage->premium = $sub['unitPremium'] ?? '';
                $subCoverage->rate    = (string)($sub['rate'] ?? '');

                $amount  += $subCoverage->amount;
                $premium += $subCoverage->premium;

                $productPrinter->subCoverageAppend($subCoverage);
            }
            $productPrinter->unitAmount = (string)$amount;
            $productPrinter->premium    = (string)$premium;

            $printer->productListAppend($productPrinter);
        }

        $this->printer = $printer;
        return $this;
    }
}